﻿Imports System.Data.OleDb
Public Class TeamRating
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("soccerConnectionString").ConnectionString)
            oleDbConn.Open()
            Dim LikeStatusSql As String = "Select * From Likes Where fixtureId = @f10 and uName = @f11"
            Dim LikeStatuscmd As OleDbCommand = New OleDbCommand(LikeStatusSql, oleDbConn)
            LikeStatuscmd.CommandType = CommandType.Text
            LikeStatuscmd.Parameters.AddWithValue("@f10", Request.Params("fixtureId"))
            LikeStatuscmd.Parameters.AddWithValue("@f11", User.Identity.Name)
            Dim dr As OleDbDataReader = LikeStatuscmd.ExecuteReader()
            If dr.HasRows Then
                bn_Like.Visible = False
                lbl_like.Visible = True
            Else
                bn_Like.Visible = True
                lbl_like.Visible = False
            End If
        End If
    End Sub

    Protected Sub bn_SaveRating_Click(sender As Object, e As EventArgs) Handles bn_SaveRating.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("soccerConnectionString").ConnectionString)
        oleDbConn.Open()
        Dim delRatingSql As String = "Delete From FixtureRating Where fixtureId = @f5 and uName = @f6"
        Dim delRatingcmd As OleDbCommand = New OleDbCommand(delRatingSql, oleDbConn)
        delRatingcmd.CommandType = CommandType.Text
        delRatingcmd.Parameters.AddWithValue("@f5", Request.Params("fixtureId"))
        delRatingcmd.Parameters.AddWithValue("@f6", User.Identity.Name)
        delRatingcmd.ExecuteNonQuery()

        Dim addRatingSql As String = "Insert Into FixtureRating(fixtureId,rating,uName,dateRated) values (@f1,@f2,@f3,@f4)"
        Dim cmd As OleDbCommand = New OleDbCommand(addRatingSql, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", Request.Params("fixtureId"))
        cmd.Parameters.AddWithValue("@f2", Rating1.CurrentRating)
        cmd.Parameters.AddWithValue("@f3", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f4", DateTime.Now.ToString("dd/MM/yyyy HH:mm"))

        cmd.ExecuteNonQuery()
        Response.Redirect("resultconfirm.aspx")
    End Sub

    Protected Sub bn_Like_Click(sender As Object, e As EventArgs) Handles bn_Like.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("soccerConnectionString").ConnectionString)
        oleDbConn.Open()
        Dim addLikeSql As String = "Insert Into Likes(fixtureId,uName,dateLiked) values (@f7,@f8,@f9)"
        Dim likecmd As OleDbCommand = New OleDbCommand(addLikeSql, oleDbConn)
        likecmd.CommandType = CommandType.Text
        likecmd.Parameters.AddWithValue("@f7", Request.Params("fixtureId"))
        likecmd.Parameters.AddWithValue("@f8", User.Identity.Name)
        likecmd.Parameters.AddWithValue("@f9", DateTime.Now.ToString("dd/MM/yyyy HH:mm"))
        likecmd.ExecuteNonQuery()
        bn_Like.Visible = False
        lbl_like.Visible = True
    End Sub
End Class